﻿#include "bpt_node.h"
#include <stdio.h>
#include "bpt_leaf.h"
#include "bpt_inner_node.h"

bool BPTNode::is_leaf() {
	return false;
}

BPTNode::BPTNode() {
	size = 0;
}

void BPTNode::print() {
	printf("Abstract BPTNode");
}

BPTNode* BPTNode::deserialize(char* serialization, int branchs, int leaf_cap) {
	/*序列化格式说明-按字节顺序
	* sizeof(int)			: 序列化长度
	* 1						: 标志字节
	* sizeof(int)			: size字段
	* sizeof(ulong)*size	: keys数组
	* remaining size		: 其他Leaf/InnerNode独有字段
	*/
	char* org_seria = serialization;
	serialization += sizeof(int);
	char flags = serialization[0];
	serialization += 1;

	if (flags & BPT_LEAF) {
		BPTLeaf* leaf = new BPTLeaf(leaf_cap);
		if (leaf->load(org_seria)) {
			return leaf;
		}
	}
	else if (flags & BPT_INNER_NODE) {
		BPTInnerNode* node = new BPTInnerNode(branchs);
		if (node->load(org_seria)) {
			return node;
		}
	}

	return nullptr;
}